<template>
  <div :class="[{hover:hover},'chat-item']" @touchstart="hover=true" @touchend="hover=false" v-on="$listeners">
    <van-badge :content="unreadCount" class="avatar-wrapper">
      <van-image :src="avatar" radius="8" />
    </van-badge>
    <div class="value-name van-hairline--bottom">
      <div class="chat-up">
        <div class="name">{{name}}</div>
        <div class="time">{{timeStr}}</div>
      </div>
      <div class="chat-down">
        <div class="msg">{{msg}}</div>
        <OnlineStatus :status="online" :size="12"/>
      </div>
    </div>
  </div>
</template>

<script>
import { formatTime } from '@/utils'
import OnlineStatus from '@/components/OnlineStatus.vue'
export default {
  name: 'ChatItem',
  components: { OnlineStatus },
  props: {
    avatar: String,
    name: String,
    msg: String,
    time: String,
    unread: Number,
    online: Number
  },
  data() {
    return {
      hover: false
    }
  },
  computed: {
    unreadCount() {
      if (!this.unread) {
        return null
      }
      if (this.unread > 99) {
        return '...'
      }
      return this.unread
    },
    timeStr() {
      const tm = formatTime(this.time)
      return tm
    }
  },
  methods: {
    touchstart() {
      console.log('touchstart')
    },
    touchend() {
      console.log('touchend')
    }
  }
}
</script>

<style lang="scss" scoped>
.chat-item {
  @include forbid-select;
  padding: 0 $padding-side;
  display: flex;
  align-items: center;
  .avatar-wrapper {
    flex-grow: 0;
    margin-right: 10px;
    height: 50px;
    .van-image {
      width: 50px;
      height: 50px;
    }
  }
  .value-name {
    @include forbid-select;
    text-align: left;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    cursor: default;
    padding: $padding-side 0;
    .chat-up {
      display: flex;
      justify-items: center;
      line-height: 30px;
      .name {
        flex: 1;
        @include textoverflow;
        color: $text-color;
        font-size: $font-size-lg;
      }
      .time {
        flex-basis: 90px;
        font-size: $font-size-sm;
        text-align: right;
      }
    }
    .chat-down {
      display: flex;
      line-height: 20px;
      .msg {
        flex: 1;
        @include textoverflow;
        color: $gray-6;
        font-size: $font-size-sm;
        line-height: 1.5em;
        height: 1.5 * $font-size-sm;
      }
    }
  }
}

.hover {
  background-color: $gray-3;
}
</style>
